package com.yuyang.wifi.views.recyclerview.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.yuyang.wifi.views.recyclerview.exception.BaseRecyclerViewException;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by tik on 17/8/8.
 */

public class BaseRecyclerView extends RecyclerView {
	private boolean mMoveState = false;
	private int mIndex = 0;
	private boolean smoothScroll;
	private RecyclerViewListener mRecyclerViewListener;

	public LinearLayoutManager getLinearLayoutManager() {
		LayoutManager layoutManager = getLayoutManager();
		if (layoutManager instanceof LinearLayoutManager){
			return (LinearLayoutManager) getLayoutManager();
		}
		throw new BaseRecyclerViewException("非LinearLayoutManager");
	}

	public int findLastVisibleItemPosition(){
		return getLinearLayoutManager().findLastVisibleItemPosition();
	}

	public int findFirstVisibleItemPosition(){
		return getLinearLayoutManager().findFirstVisibleItemPosition();
	}

	public void setLinearLayoutManager(){
		setLayoutManager(new LinearLayoutManager(getContext()));
	}

	public BaseRecyclerView(Context context) {
		super(context);
		init(context);
	}

	public BaseRecyclerView(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public BaseRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	protected void init(Context context) {
	}

	public void checkRecyclerViewListener() {
		if (mRecyclerViewListener == null){
			mRecyclerViewListener = createRecyclerViewListener();
			addOnScrollListener(mRecyclerViewListener);
		}
	}

	protected RecyclerViewListener createRecyclerViewListener(){
		return new RecyclerViewListener(this);
	}

	public void afterSmoothMoveToPosition(final int n) {
		ViewPreparedUtil.addViewPreparedListener(this, new ViewPreparedUtil.OnViewPreparedListener() {
			@Override
			public void onFirstPrepared(View view) {
				smoothMoveToPosition(n);
			}
		});
	}

	public void smoothMoveToPosition(int n) {
		checkRecyclerViewListener();
		stopScroll();
		mIndex = n;
		smoothScroll = true;
		int firstItem = findFirstVisibleItemPosition();
		int lastItem = findLastVisibleItemPosition();
		if (n <= firstItem ){
			smoothScrollToPosition(n);
		}else if ( n <= lastItem ){
			int top = getChildAt(n - firstItem).getTop();
			smoothScrollBy(0, top);
		}else{
			smoothScrollToPosition(n);
			mMoveState = true;
		}
	}

	public void afterMoveToPosition(final int n) {
		ViewPreparedUtil.addViewPreparedListener(this, new ViewPreparedUtil.OnViewPreparedListener() {
			@Override
			public void onFirstPrepared(View view) {
				moveToPosition(n);
			}
		});
	}

	public void moveToPosition(int n) {
		checkRecyclerViewListener();
		stopScroll();
		mIndex = n;
		smoothScroll = false;
		int firstItem = findFirstVisibleItemPosition();
		int lastItem = findLastVisibleItemPosition();
		if (n <= firstItem ){
			scrollToPosition(n);
		}else if ( n <= lastItem ){
			int top = getChildAt(n - firstItem).getTop();
			scrollBy(0, top);
		}else{
			scrollToPosition(n);
			mMoveState = true;
		}

	}


	public static class RecyclerViewListener extends OnScrollListener {
		protected BaseRecyclerView mBaseRecyclerView;

		public RecyclerViewListener(BaseRecyclerView baseRecyclerView) {
			mBaseRecyclerView = baseRecyclerView;
		}

		@Override
		public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
			super.onScrollStateChanged(recyclerView, newState);
			if (mBaseRecyclerView.mMoveState && newState == RecyclerView.SCROLL_STATE_IDLE && mBaseRecyclerView.smoothScroll){
				mBaseRecyclerView.mMoveState = false;
				int n = mBaseRecyclerView.mIndex - mBaseRecyclerView.findFirstVisibleItemPosition();
				if ( 0 <= n && n < mBaseRecyclerView.getChildCount()){
					int top = mBaseRecyclerView.getChildAt(n).getTop();
					mBaseRecyclerView.smoothScrollBy(0, top);
				}
			}
		}

		@Override
		public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
			super.onScrolled(recyclerView, dx, dy);
			if (mBaseRecyclerView.mMoveState && !mBaseRecyclerView.smoothScroll){
				mBaseRecyclerView.mMoveState = false;
				int n = mBaseRecyclerView.mIndex - mBaseRecyclerView.findFirstVisibleItemPosition();
				if ( 0 <= n && n < mBaseRecyclerView.getChildCount()){
					int top = mBaseRecyclerView.getChildAt(n).getTop();
					mBaseRecyclerView.scrollBy(0, top);
				}
			}
		}
	}


}
